﻿using System.Threading.Tasks;
using System;
using Microsoft.EntityFrameworkCore;
using Oracle.ManagedDataAccess.Client;
using Microsoft.AspNetCore.Http;
using Simple.Services.System;
using Simple.Services.System.QHSEUser.Models;
using Simple.Services.System.QHSEViolationCategory.Models;

namespace Simple.Services;

public class QHSELocationService
{
    private readonly SimpleDbContext _context;
    public QHSELocationService(SimpleDbContext context)
    {
        _context = context;
    }
    public async Task<List<QHSELocationModel>> GetAsync()
    {
        var keyWorkTypes = await _context.Set<QHSESysLocation>().AsNoTracking().ToListAsync();
        return MapperHelper.Map<List<QHSELocationModel>>(keyWorkTypes);
    }
    public async Task<string> GetCodeByItemAsync(string item)
    {
        var id = await _context.Set<QHSESysLocation>().AsNoTracking().Where(u => u.Location.Equals(item)).Select(u => u.LocationCode).FirstOrDefaultAsync();
        return id;
    }
    public async Task<PageResultModel<QHSELocationModel>> GetPageAsync(QHSELocationPageInputModel input)
    {
        var result = new PageResultModel<QHSELocationModel>();
        var query = _context.Set<QHSESysLocation>().AsNoTracking().AsQueryable();

        if (!string.IsNullOrEmpty(input.Code))
        {
            
            if(input.Code == "SP1014" || input.Code == "SP1013" || input.Code == "SP1015")
            {
                var allLocations =await _context.Set<QHSESysLocation>().AsNoTracking().GroupBy(location => location.Location)
                    .Select(group => group.First())
                    .ToListAsync(); ;
                result.Rows = MapperHelper.Map<List<QHSELocationModel>>(allLocations);
                result.SetPage(input);
                result.CountTotalPage(); MapperHelper.Map<List<QHSELocationModel>>(allLocations);
                return result;
            }
            query = query.Where(u => EF.Functions.Like(u.SpecialtyCode, $"%{input.Code}%"));

        }
        // 获取总数量
        result.TotalRows = await query.CountAsync();

        // 分页查询
        input.PageSize = result.TotalRows;
        query = query.OrderBy(u => u.OrderNo).Page(input.PageNo, input.PageSize);
        var keyWorkTypes = await query.ToListAsync();
        result.Rows = MapperHelper.Map<List<QHSELocationModel>>(keyWorkTypes);

        result.SetPage(input);
        result.CountTotalPage();

        return result;
    }
    public async Task<List<QHSELocationModel>> GetLocationAsync(string workSpeciality)
    {
        var result = new List<QHSELocationModel>();
        
        var query = _context.Set<QHSESysLocation>()
            .AsNoTracking()
            .Join(_context.Set<QHSESysKeyWorkSpecialty>(), // Join with Organization table
                        location => location.SpecialtyCode,
                        workSpeciality => workSpeciality.SpecialtyCode,
                        (location, workSpeciality) => new { Location = location, WorkSpeciality = workSpeciality })
            .Where(u => u.WorkSpeciality.Specialty == workSpeciality)
            .Select(joinResult => joinResult.Location)
             .AsQueryable();
        var locations = await query.ToListAsync();
        result = MapperHelper.Map<List<QHSELocationModel>>(locations);

        return result;
    }
}
